package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.user.req.agent.*;
import com.dd.cloud.user.res.agent.*;
import com.dd.cloud.user.service.agent.IAgentService;
import com.dd.cloud.user.service.agent.IAgentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author Lx
 * @version 1.0
 */
@Validated
@RestController
@RequestMapping("/agent")
public class AgentController {
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentUserService agentUserService;

    /**
     * 创建代理商
     *
     * @param req
     */
    @PostMapping("/create")
    public ResponseEntity<Void> createAgent(@Valid @RequestBody AgentCreateReq req) {
        agentService.createAgent(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 创建代理商 h5适用
     *
     * @param req
     */
    @PostMapping("/create/h5")
    public ResponseEntity<Void> createAgentH5(@Valid @RequestBody AgentCreateH5Req req) {
        agentService.createAgentH5(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改代理商
     *
     * @param req
     */
    @PostMapping("/update")
    public ResponseEntity<Void> updateAgent(@Valid @RequestBody AgentUpdateReq req) {
        agentService.updateAgent(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询代理商列表 分页
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<AgentPageRes>> queryAgentPage(@Valid AgentPageReq req) {
        IPage<AgentPageRes> res = agentService.queryAgentPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询代理商详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public ResponseEntity<AgentDetailRes> queryAgentDetail(Integer id) {
        AgentDetailRes res = agentService.queryAgentDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询代理商代理医生列表 分页
     *
     * @param req
     * @return
     */
    @GetMapping("/doctor/page")
    public ResponseEntity<IPage<AgentDoctorPageRes>> queryAgentDoctorPage(@Valid AgentDoctorPageReq req) {
        IPage<AgentDoctorPageRes> res = agentService.queryAgentDoctorPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询代理商代理机构列表 分页
     *
     * @param req
     * @return
     */
    @GetMapping("/mechan/page")
    public ResponseEntity<IPage<AgentMechanPageRes>> queryAgentMechanPage(@Valid AgentMechanPageReq req) {
        IPage<AgentMechanPageRes> res = agentService.queryAgentMechanPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 修改代理商状态
     *
     * @param id
     * @param status
     */
    @PostMapping("/update/status")
    public ResponseEntity<Void> updateAgentStatus(@NotNull(message = "代理商id不能为空") Integer id,
                                                  @NotNull(message = "代理商状态不能为空") Integer status) {
        agentService.updateAgentStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 代理商用户登录 密码方式
     *
     * @param request
     * @param response
     * @param req
     * @return
     */
    @PostMapping("/user/login/pwd")
    public ResponseEntity<AgentUserLoginRes> loginAgentUserPwd(HttpServletRequest request, HttpServletResponse response,
                                                               @Valid AgentUserLoginPwdReq req) {
        AgentUserLoginRes res = agentUserService.loginAgentUserPwd(request, response, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 代理商用户登录 验证码方式
     *
     * @param request
     * @param response
     * @param req
     * @return
     */
    @PostMapping("/user/login/code")
    public ResponseEntity<AgentUserLoginRes> loginAgentUserCode(HttpServletRequest request, HttpServletResponse response,
                                                                @Valid AgentUserLoginCodeReq req) {
        AgentUserLoginRes res = agentUserService.loginAgentUserCode(request, response, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 代理商用户找回密码
     *
     * @param req
     */
    @PostMapping("/user/forget/pwd")
    public ResponseEntity<Void> forgetPwdAgentUser(@Valid @RequestBody AgentUserForgetPwdReq req) {
        agentUserService.forgetPwdAgentUser(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 代理商用户通过旧密码修改密码
     *
     * @param req
     */
    @PostMapping("/user/update/pwd")
    public ResponseEntity<Void> updateAgentUser(@Valid AgentUserUpdatePwdReq req) {
        agentUserService.updateAgentUser(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 统计首页邀请机构/医生数量图
     *
     * @param req
     * @return
     */
    @GetMapping("/statistics/count")
    public ResponseEntity<AgentCountChartStatisticsRes> queryAgentCountStatistics(@Valid AgentCountChartStatisticsReq req) {
        AgentCountChartStatisticsRes res = agentService.queryAgentCountStatistics(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 初始化代理商邀请码
     */
    @PostMapping("/init/invite/code")
    public ResponseEntity<Void> initAgentInviteCode() {
        agentService.initAgentInviteCode();
        return ResponseEntity.ok().build();
    }
}
